package com.ihighest.app.infox.activity;

import org.json.JSONArray;
import org.json.JSONException;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.TextView;

import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
import com.ihighest.app.common.api.Api;
import com.ihighest.app.common.api.ApiCallback;
import com.ihighest.app.common.api.ApiParam;
import com.ihighest.app.common.api.ApiResult;
import com.ihighest.app.common.kit.AppKit;
import com.ihighest.app.common.kit.ToastKit;
import com.ihighest.app.infox.Const;
import com.ihighest.app.infox.R;
import com.ihighest.app.infox.adapter.SelecMyColumnListAdapter;
import com.ihighest.app.infox.myview.ActivityTitle;

/**
 * 企业号界面
 * 
 * @author lkx Created at 2015-10-14 上午9:42:26
 * @version 1.0
 */
public class EnterpriseNumberActivity extends BaseActivity implements OnClickListener {
	private ActivityTitle title;
	// private MySubscriptionListAdapter adapter;SelecMyColumnListAdapter
	private SelecMyColumnListAdapter adapter;
	private PullToRefreshListView enterpriselistview;
	private int page = 1;// 加载数据的页数，默认为第一页
	private long time;
	private JSONArray array;
	private JSONArray moreArray;
	private TextView noData;
	private ReflashBroadcast reflashBroadcast;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.infox_activity_enterpeise_number);
		initView();
	}

	@SuppressWarnings({ "unchecked", "rawtypes" })
	private void initView() {
		title = (ActivityTitle) findViewById(R.id.enterpriseNumberActivityTitle);
		title.setTitle("企业号");
		title.setViewGone(title.titleMsg, title.titleOther, title.rightLayout, title.titleSearch);
		title.setViewVersible(title.titleBack);
		enterpriselistview = (PullToRefreshListView) findViewById(R.id.enterprisenumberlistView);
		noData = (TextView) findViewById(R.id.noData);
		findViewById(R.id.attentionMore).setOnClickListener(this);
		reflashBroadcast = new ReflashBroadcast();
		IntentFilter filter = new IntentFilter();
		filter.addAction(Const.REFLASH);
		this.registerReceiver(reflashBroadcast, filter);
		adapter = new SelecMyColumnListAdapter(EnterpriseNumberActivity.this);
		enterpriselistview.setAdapter(adapter);

		initData();
		enterpriselistview.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2() {

			@Override
			public void onPullDownToRefresh(PullToRefreshBase refreshView) {
				page = 1;
				initData();
			}

			@Override
			public void onPullUpToRefresh(PullToRefreshBase refreshView) {
				page++;
				initData();
			}

		});

		enterpriselistview.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3) {

				Intent intent = new Intent(EnterpriseNumberActivity.this, CompanyPagesActivity.class);
				try {
					intent.putExtra("cid", array.getJSONObject(position - 1).getString("id"));
					startActivity(intent);
				} catch (JSONException e) {
					e.printStackTrace();
				}
			}
		});
	}
	
	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		page = 1;
		initData();
	}

	/**
	 * 获取企业号列表
	 * 
	 * 
	 * @author lkx Created at 2015-10-30 下午4:35:50
	 */
	private void initData() {
		ApiParam param = ApiParam.create();
		param.addParam("cid", "cid");
		param.addParam("type", "e");
		param.addParam("page", page);
		Api.get("channel/subscribes", param, new ApiCallback(EnterpriseNumberActivity.this,true) {

			@Override
			public void handleSuccess(ApiResult result) {
				if (result.getDatas() == null || result.getDatas().length() == 0) {
					if (page == 1) {
						// 提示没数据
						noData.setVisibility(View.VISIBLE);
						enterpriselistview.setVisibility(View.GONE);
					} else {
						// 提示数据加载完毕
						noData.setVisibility(View.GONE);
						enterpriselistview.setVisibility(View.VISIBLE);
					}

				} else {
					noData.setVisibility(View.GONE);
					enterpriselistview.setVisibility(View.VISIBLE);
					if (page == 1) {
						array = result.getDatas();
						adapter = new SelecMyColumnListAdapter(EnterpriseNumberActivity.this, result.getDatas());
						enterpriselistview.setAdapter(adapter);
					} else {

						try {
							moreArray = result.getDatas();
							array = AppKit.joinJSONArray(array, moreArray);
							adapter.appendData(moreArray);
						} catch (JSONException e) {
							e.printStackTrace();
						}
					}
				}

				enterpriselistview.onRefreshComplete();

			}

			@Override
			public void handleFailure(Exception e, String msg) {
				enterpriselistview.onRefreshComplete();
			}
		}, EnterpriseNumberActivity.this);

	}

	@Override
	public void onClick(View view) {
		switch (view.getId()) {
		case R.id.attentionMore:
			ToastKit.showShort(EnterpriseNumberActivity.this, Const.UN_FINISH_ALERT);
			break;
		}

	}

	public class ReflashBroadcast extends BroadcastReceiver {

		@Override
		public void onReceive(Context context, Intent intent) {
			// 得到数据变化的广播
			if (intent.getAction().equals(Const.REFLASH)) {
				page = 1;

				initData();

			}

		}

	}
}
